<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>注册 - 慰风博客</title>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
        [v-cloak] {
            display: none;
        }
        body {
            margin: 0;
            padding: 0;
            background-color: #f5f7fa;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
        }
        .register-container {
            max-width: 400px;
            margin: 100px auto;
            padding: 20px;
        }
        .register-title {
            text-align: center;
            margin-bottom: 30px;
            font-size: 24px;
            color: #333;
        }
        .register-form {
            background: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
        }
        .register-actions {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div id="app" v-cloak>
        <div class="register-container">
            <h1 class="register-title">注册账号</h1>
            <div class="register-form">
                <el-form ref="registerForm" :model="registerForm" :rules="rules">
                    <el-form-item prop="username">
                        <el-input v-model="registerForm.username" placeholder="用户名">
                            <template slot="prepend">
                                <i class="el-icon-user"></i>
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input v-model="registerForm.password" type="password" placeholder="密码">
                            <template slot="prepend">
                                <i class="el-icon-lock"></i>
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="confirmPassword">
                        <el-input v-model="registerForm.confirmPassword" type="password" placeholder="确认密码">
                            <template slot="prepend">
                                <i class="el-icon-lock"></i>
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="email">
                        <el-input v-model="registerForm.email" placeholder="邮箱">
                            <template slot="prepend">
                                <i class="el-icon-message"></i>
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="nickname">
                        <el-input v-model="registerForm.nickname" placeholder="昵称">
                            <template slot="prepend">
                                <i class="el-icon-user"></i>
                            </template>
                        </el-input>
                    </el-form-item>
                    <div class="register-actions">
                        <el-button type="text" @click="goToLogin">返回登录</el-button>
                        <el-button type="primary" @click="handleRegister" :loading="loading">注册</el-button>
                    </div>
                </el-form>
            </div>
        </div>
    </div>

    <script>
        new Vue({
            el: '#app',
            data() {
                const validatePass = (rule, value, callback) => {
                    if (value === '') {
                        callback(new Error('请输入密码'));
                    } else {
                        if (this.registerForm.confirmPassword !== '') {
                            this.$refs.registerForm.validateField('confirmPassword');
                        }
                        callback();
                    }
                };
                const validatePass2 = (rule, value, callback) => {
                    if (value === '') {
                        callback(new Error('请再次输入密码'));
                    } else if (value !== this.registerForm.password) {
                        callback(new Error('两次输入密码不一致!'));
                    } else {
                        callback();
                    }
                };
                const validateEmail = (rule, value, callback) => {
                    const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
                    if (value === '') {
                        callback(new Error('请输入邮箱'));
                    } else if (!emailRegex.test(value)) {
                        callback(new Error('请输入正确的邮箱格式'));
                    } else {
                        callback();
                    }
                };
                return {
                    registerForm: {
                        username: '',
                        password: '',
                        confirmPassword: '',
                        email: '',
                        nickname: ''
                    },
                    rules: {
                        username: [
                            { required: true, message: '请输入用户名', trigger: 'blur' },
                            { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
                        ],
                        password: [
                            { required: true, validator: validatePass, trigger: 'blur' },
                            { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
                        ],
                        confirmPassword: [
                            { required: true, validator: validatePass2, trigger: 'blur' }
                        ],
                        email: [
                            { required: true, validator: validateEmail, trigger: 'blur' }
                        ],
                        nickname: [
                            { required: true, message: '请输入昵称', trigger: 'blur' },
                            { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
                        ]
                    },
                    loading: false
                }
            },
            methods: {
                async handleRegister() {
                    try {
                        await this.$refs.registerForm.validate();
                        this.loading = true;
                        const res = await axios.post('/api/users/register', {
                            username: this.registerForm.username,
                            password: this.registerForm.password,
                            email: this.registerForm.email,
                            nickname: this.registerForm.nickname
                        });
                        if (res.data.code === 200) {
                            this.$message.success('注册成功');
                            setTimeout(() => {
                                window.location.href = '/login.html';
                            }, 1500);
                        } else {
                            this.$message.error(res.data.message || '注册失败');
                        }
                    } catch (error) {
                        if (error.response) {
                            this.$message.error(error.response.data.message || '注册失败');
                        } else {
                            this.$message.error('注册失败');
                        }
                        console.error('注册失败:', error);
                    } finally {
                        this.loading = false;
                    }
                },
                goToLogin() {
                    window.location.href = '/login.html';
                }
            }
        });
    </script>
</body>
</html> 